package com.zx.mes.senior.classloader;

import org.junit.Test;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

/**
 * classloader test
 *
 * @author 华云龙
 * @date 2018-9-26
 */
public class ClassLoaderTest {

    @Test
    public void test(){
        /// 只查询当前位置是否有application.properties  Test.class
        /// 实际上查的是target下的文件,那么要注意非java文件不会被打包,需要特别指定
        URL resource = ClassLoaderTest.class.getResource("application.properties");
        System.out.println(resource);
        URL resource2 = ClassLoaderTest.class.getResource("Test.class");
        System.out.println(resource2);
    }


    @Test
    public void test2(){
        /// classpath:下查找文件
        URL resource = ClassLoaderTest.class.getResource("/application.properties");
        System.out.println(resource);
        /// classpath
        URL resource2 = ClassLoaderTest.class.getResource("/");
        /// 当前路径
        URL resource3 = ClassLoaderTest.class.getResource("");
        System.out.println(resource2);
        System.out.println(resource3);

    }


    @Test
    public void test3(){
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        System.out.println(classLoader.getResource("application.properties"));
    }

    @Test
    public void test4() throws IOException {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        /// 找寻所有的spring.factories
        Enumeration<URL> urls= classLoader.getResources("META-INF/spring.factories");
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            System.out.println(url);
        }
    }
}
